// index.js
var app = getApp()
const request = require('../../utils/request')

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    isShowMask: true,
    bannerList: [
      '/img/ban.png',
      '/img/ban.png',
      '/img/ban.png'
    ],
    indicatorDots: true,  //是否显示面板指示点
    autoplay: true,      //是否自动切换
    interval: 3000,       //自动切换时间间隔
    duration: 1000,       //滑动动画时长
    configInfo: {},
  },

  onLoad: function () {
    this.getBanner()
  },

  getBanner(){
    wx.showLoading()
    request.get('banner_list')
      .then(res => {
        wx.hideLoading()
        if (res.data && res.data.length) {
          res.data.forEach(v => {
            const regex = /\\/g
            const url = v.banner_img
            if (regex.test(url)) {
              v.banner_img = url.replace(/\\/g, '/')
            }
          })

          this.setData({
            bannerList: res.data
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  // 获取用户信息
  getUserLogin: function (res) {
    if (res.detail.errMsg == 'getUserInfo:ok') {
      var userInfo = res.detail.userInfo
      wx.request({
        url: app.d.severUrl + '/WxSet/postMemberData',
        data: {
          openid: app.d.openId,
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl,
          gender: userInfo.gender,
        },
        method: 'post',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: res => {
          if (res.data.code == 0) {
            //error
          } else {
            app.d.getMemberData = res.data.data
            //跳转到绑定二维码页面
            wx.navigateTo({
              url: '/pages/userCenter/bindCode/bindCode'
            })
          }
        }
      })
    } else if (res.detail.errMsg == 'getUserInfo:fail auth deny') {
      wx.showModal({
        title: '温馨提示',
        content: '拒绝授权可能会影响您的体验，点击确认立即重新获取授权。',
        duration: 2000,
        mask: true
      })
    }
  },

  callService() {
    const { configInfo } = this.data
    if (Object.keys(configInfo).length) {
      wx.makePhoneCall({
        phoneNumber: configInfo.phone || ''
      })
    } else {
      this.getConfig()
    }
  },

  getConfig() {
    wx.showLoading()
    return request.get('config')
      .then(res => {
        wx.hideLoading()
        if (res.data) {
          this.setData({
            configInfo: res.data
          })

          wx.makePhoneCall({
            phoneNumber: res.data.phone || ''
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  navigateTo(e) {
    const target = e.currentTarget.dataset.target
    const pages = {
      newProduct: '../index/kehu/kehu',
      consult: '../index/weixiu/weixiu',
      carwash: '../index/clear/clear',
      talent: '../index/talentMarket/announceList',
      carNotice: '../index/carNotice/carNotice',
      repair: '../index/weixiuHome/weixiuHome',
      staff: '../index/staff/staff',
      rescue: '../index/rescue/rescue',
      carGuanggao: '../market/xitong/xitong?type_id=58&type_name=汽车广告',
      newProduct: '../market/xitong/xitong?type_id=59&type_name=当季新品',
      oldProduct: '../market/xitong/xitong?type_id=60&type_name=旧物市场'
    }
    console.log(pages[target])
    wx.navigateTo({
      url: pages[target]
    })
  },

  seekHelp() {
    
  },

  toMarket() {
    wx.switchTab({
      url: '/pages/market/market'
    })
  }
})
